<?php
class paginator
{
	static $curentpage;	
	static $item_perpage = 6;
	static $totalpage;

	public function paginate($mysql)
	{
		if(isset($_REQUEST['page']))
		{
			self::$curentpage = $_REQUEST['page'];
		}
		else
		{
			self::$curentpage= 1;
		}
		
		//get all product
		$items = DataProvider::ExecuteQuery($mysql);
		$total_item = mysql_num_rows($items);
		if($total_item ==0)
		echo "<h3>No any items</h3>";
		else
		{
		
			//get limit
			$start = (self::$curentpage -1) * self::$item_perpage;
			$query = $mysql." limit ".$start.",".self::$item_perpage;
			$result = DataProvider::ExecuteQuery($query);				
			self::$totalpage = ceil($total_item / self::$item_perpage);
			
			self::display($result);
			
			if(self::$totalpage != 1)
			{
				self::page_bar();
			}
		}
	}
	
	public static function display($items)
	{
		while($row = mysql_fetch_array($items))
		{  
			echo  '     
			<table width="207" height="234"  border="0" cellpadding="0" cellspacing="0" class="productleft_top">
			  <tr>
				<td align="left" valign="top" class="newtovar">
					<table width="207"  border="0" cellspacing="0" cellpadding="0">
					  <tr>
						<td height="200" valign="top">
							<table width="100%"  border="0" cellspacing="0" cellpadding="0">
							  <tr>
								<td height="53">
									<span class="style7">
										<a href="#">'.$row["TenSanPham"].'</a>
									</span>
								</td>
							  </tr>
							  <tr>
								<td height="147" align="center">
									<a href="#"><img src="images/'.$row["HinhURL"].'" alt="" width="175" height="144" border="0"></a>
								</td>
							  </tr>
							</table>
						</td>
						</tr>
						<tr>
						   <td height="35">
							<table width="100%"  border="0" cellspacing="0" cellpadding="0">
							  <tr>
								<td width="38%" height="24">
									<span class="style7">'.substr($row["MoTa"], 0, 30).'...</span>
								</td>
								<td width="23%">&nbsp;</td>
								<td width="39%" class="price">'.$row["GiaSanPham"].' USD                        
								</td>
							  </tr>
							  <tr>
								<td width="38%" height="24" colspan="3" align="center">                    	
									<a href="index.php?show=buynow&id='.$row["MaSanPham"].'" id="blue1">
									 Buy Now  
									</a>
									&nbsp|&nbsp;
									<a href="index.php?show=details&id='.$row["MaSanPham"].'" id="blue1">
									 Details  
									</a> 
								</td>                                        
							  </tr>
							</table>
						   </td>
						 </tr>
					</table>
				</td>
			  </tr>
			</table>';
		} 
	}
	
	public static function page_bar()
	{	
		$show = $_REQUEST['show'];
		if(isset($_REQUEST['catid']))
		{
			$catid = $_REQUEST['catid'];
		}
		if(isset($_REQUEST['manuid']))
		{
			$manuid = $_REQUEST['manuid'];
		}
		echo "<br/>";
		echo '	
		<table width="100%" class="page_bar">
			<tr>
				<td align="center">';
					if(self::$curentpage != 1)
					{
						echo '<a href="index.php?show='.$show.'"> First </a>
							  <<<a href="index.php?show='.$show.'&page='.(self::$curentpage - 1).'&catid='.$catid.'&manuid='.$manuid.'"> Previous </a>&nbsp;-&nbsp';
					}				
					for($i=1; $i<=self::$totalpage; $i++)
					{
						if($i != self::$curentpage)
						{
							echo '<a href="index.php?show='.$show.'&page='.$i.'&catid='.$catid.'&manuid='.$manuid.'">'.$i.'</a>&nbsp;-&nbsp;';
						}
						else
						{
							echo '<a>'.$i.'</a>&nbsp;-&nbsp';
						}
					}
					if(self::$curentpage != self::$totalpage)
					{
						echo '<a href="index.php?show='.$show.'&page='.(self::$curentpage + 1).'&catid='.$catid.'&manuid='.$manuid.'">Next </a>>>
							  <a href="index.php?show='.$show.'&page='.self::$totalpage.'&catid='.$catid.'&manuid='.$manuid.'">End </a>';
					}
					
		echo '	</td>
			</tr>
		</table>';
	}
}
?>